package com.icehand.echarts.web.controller;

import com.icehand.echarts.model.EchartsOption;
import com.icehand.echarts.web.util.EchartImageCreator;
import com.icehand.echarts.web.util.ImageResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;

/**
 * @author zhoujunmei
 * @date 2019-08-05
 */
@RestController
public class DemoController {
    @Value("${phantomjs.path}")
    private String phantomjsPath;
    @Value("${image.path}")
    private String path;

    @PostMapping("/")
    public String create(@RequestBody EchartsOption echartsOption){
        ImageResult imageResult;
        String fileName = System.currentTimeMillis()+".png";
        try {
            imageResult = EchartImageCreator.create(phantomjsPath, path + fileName,echartsOption);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        return imageResult.getPath();
    }
}
